using System;
using UnityEngine;

public class BattleUIWidgetInfoController : BattleWidgetInfoController
{
    BattleMainUI battleUI
    {
        get
        {
            if (!this) return null;
            return GetComponentInParent<BattleMainUI>();
        }
    }
    [SerializeField]
    Transform numInfoParent;

    [SerializeField]
    NumInfoUi numInfoUiPref;

    ZTransformAnim m_ZTransformAnim;
    public ZTransformAnim transformAnim
    {
        get
        {
            if (m_ZTransformAnim == null)
            {
                m_ZTransformAnim = gameObject.AddComponent<ZTransformAnim>();
            }
            return m_ZTransformAnim;
        }
    }
    protected override void Start()
    {
        base.Start();
        
        if(numInfoUiPref)
            numInfoUiPref.gameObject.SetActive(false);
    }
 
    public NumInfoUi AddNumInfo(int showNum, Transform refer,Vector3 offset,bool moveUp, Vector3 moveTargetUVPos, bool follow, Func<Vector3, Vector3, Vector3> worldToUIPosFunc)
    {
        if (!this || !numInfoUiPref) return null;

        //GameDebug.LogError($"show num:{showNum}");

        var item = Instantiate<NumInfoUi>(numInfoUiPref, numInfoParent);
        item.gameObject.SetActive(true);
        item.Init(showNum, refer, worldToUIPosFunc, offset, moveUp, moveTargetUVPos,follow);

        item.transform.localScale = Vector3.zero;
        transformAnim.ScaleTo(item.transform, Vector3.one, 0.3f, null);
        return item;
    }
    
    public T ShowObject<T>( string pId,Action<bool,T> init,GameObject pref,Transform parent)where T:MonoBehaviour
    {
        if (!this) return null;

        bool isNewObj;
        var cache = widgetCache.GetCache<T>(pref/*npcBubblePref.gameObject*/, pId, out isNewObj);
        cache.transform.SetParent(parent/*npcBubbleParent*/);
        
        var pi = cache.GetComponent<T>();

        init?.Invoke(isNewObj,pi);

        return pi;
    }

    public void HideObject<T>(string pId )where T:MonoBehaviour
    {
        if (!this) return;
        
        var dict = widgetCache.GetDict<T>();
        if (dict.ContainsKey(pId)
            && dict[pId] != null)
        {
            Destroy(dict[pId].gameObject);
            dict.Remove(pId);
        }
    }

}